package com.dream.handler;

import com.dream.exception.ApiException;
import com.dream.wrap.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = ApiException.class)
    @ResponseBody
    public R apiException(ApiException e) {
        log.error("接口请求异常！异常信息：{}", e.getMessage(), e);
        return R.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R handleException(Exception e) {
        log.error("发生异常！异常信息：{}", e.getMessage(), e);
        return R.error();
    }

}
